<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" ></n-input>
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row:ListData) => row.id"
      :dataSource="dataform"
      ref="actionRef"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >

      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>

  </n-card>
</template>

<script lang="ts" setup>
  import {  onMounted, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { columns, ListData } from './columns';
  import { getDfmeCfce, getPlaneInfo } from '@/api/dataInfo/flightInfo';
  import { ResultEnum, ipAddress } from '@/enums/httpEnum';
  import {  useMessage } from 'naive-ui';
  const options = ref([]);
  const message = useMessage();
  const schemas: FormSchema[] = [
    {
      field: 'cftp',
      component: 'NSelect',
      label: '飞机型号',
      componentProps: {
        placeholder: '请选择类型',
        options: [],
        filterable: 'true',
        // on-focus: "getList",
      },
    }
  ];

  onMounted(
    async () => {
      const response = await getPlaneInfo();

      // 将角色列表转换为 NSelect 组件的选项格式
      const CorOptions = response.data.map((plane) => ({
        label: plane.planeName,
        value: plane.planeName,
      }));

      options.value = CorOptions;

      // 找到角色类型字段的 schema 索引
      const corSchemaIndex = schemas.findIndex((schema) => schema.field === 'cftp');

      // 如果找到了角色类型字段的 schema，则将角色选项设置到 componentProps.options 中
      if (corSchemaIndex !== -1) {
        schemas[corSchemaIndex].componentProps.options = CorOptions;
      }

    } 
  );

  const actionRef = ref();
  const dataform = ref();




  // function getList(){
  //   console.log("sss")
  // }

  const loadDataTable = async (res) => {

    const rese = await getDfmeCfce({ ...getFieldsValue(), ...res });
    if(rese.code == ResultEnum.SUCCESS){
      var matchedName = ipAddress[rese.msg];
      message.success('正在访问'+matchedName+'的接口');
    }
    return rese.data;
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  async function handleSubmit() {
    reloadTable();
}

  const [register, { getFieldsValue }] = useForm({
      gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
      labelWidth: 80,
      schemas,
    });

  function handleReset(values: Recordable) {
    console.log(values);
  }

  

</script>

<style lang="less" scoped></style>
